package com.wanxi.controller;

import com.wanxi.model.StudentModel;

import java.util.ArrayList;
import java.util.Scanner;

public class MyArrayList {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        ArrayList<StudentModel> arrayList = new ArrayList<>();

        System.out.println("请输入大小？");
        int size = scanner.nextInt();
        System.out.println(arrayList.size());
        for (int i = 0; i < size; i++) {
            System.out.println("请输入学生信息，格式如：张三,23");
            String str = scanner.next();
            StudentModel model = setStudentModel(str);
//        添加对象到集合
            arrayList.add(model);
        }
        System.out.println(arrayList);
    }

    /**
     * 将得到控制台的字符串解析为学生对象
     * 张三,23
     *
     * @param str
     * @return
     */
    private static StudentModel setStudentModel(String str) {
        StudentModel model = new StudentModel();
        model.setName(str.split(",")[0]);
        String ageStr = str.split(",")[1];
        int age = Integer.parseInt(ageStr);
        model.setAge(age);
        return model;
    }
}
